import { connect } from 'react-redux';
import UI from './UI';
import action from './action';
import store from '@/store';
import { Toast } from 'antd-mobile';
const mapStateToProps = (state) => {
  // console.log(state.home.list)
  return {
    topList : state.home.topList,
    tltle: state.home.Title,
    newsList: state.home.list,
    page: state.home.page,
    allPage: state.home.allPage
  }
}
const mapDispatchToProps = (dispatch) => {
  return {
    getTopList : () => {
      dispatch(action.getTopList)
    },
    updataList : (item) => {
      // console.log(item,store.getState().home.title)
      if (item === store.getState().home.title){

      } else {
        dispatch({
          type:'CHANGE_TITLE',
          data: item
        })
        store.dispatch({
          type:'CHANGE_PAGE',
          data:1
        })
      }
      store.dispatch(action.updateList)
    },
    goDetail (item) {
      // console.log(item.newsId)
      this.history.push(`/detail/${item.newsId}`)
    },
    goUser () {
      this.history.push(`/login`)
    },
    loadMore () {
      let page = store.getState().home.page
      page++
      if (page> store.getState().home.allPage){
        Toast.info('没有更多了！')
      } else {
        // console.log(page)
        store.dispatch({
          type:'CHANGE_PAGE',
          data:page
        })
        store.dispatch(action.updateList)
      }
    }
  }
}
const Home =connect (
  mapStateToProps,
  mapDispatchToProps
)(UI)
export default Home